package com.jxt.myjxtmanager.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.MainActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.db.SaleUserModel;
import com.jxt.myjxtmanager.ui.inter.LoginInter;
import com.jxt.myjxtmanager.ui.presenter.LoginPresenter;

/**
 * 登陆界面
 *
 * @author lkk
 */
public class LoginActivity extends BaseActivity implements LoginInter {

    private TextInputEditText ce_login_username, ce_login_password;
    private LoginPresenter presenter;
    private Button bt_check_commit;
    private TextView tv_forget_password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        presenter = new LoginPresenter(this, this);
        ce_login_username = (TextInputEditText) this.findViewById(R.id.ce_login_username);
        ce_login_password = (TextInputEditText) this.findViewById(R.id.ce_login_password);
        bt_check_commit = (Button) findViewById(R.id.bt_check_commit);
        tv_forget_password = (TextView) findViewById(R.id.tv_forget_password);
        bt_check_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toLogin(bt_check_commit);
            }
        });
        tv_forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, ForgetPswActivity.class));
            }
        });

        SaleUser saleUser = SaleUserModel.getInstance().loadCurrentSaleUsers();
        if (saleUser != null && !TextUtils.isEmpty(saleUser.getPhone())) {
            ce_login_username.setText(saleUser.getPhone());
        }
    }

    public static Intent getIntent(Activity act) {
        return new Intent(act, LoginActivity.class);
    }

    public void toLogin(View view) {
        SaleUser user = SaleUserModel.getInstance().loadCurrentSaleUsers();
        long sale_id = user.getUser_id() == null ? 0 : user.getUser_id();
        if (sale_id >= 0 && !TextUtils.isEmpty(user.getPassword())) {//如果有用户信息
            if (!TextUtils.equals(user.getPhone(), ce_login_username.getText().toString().trim())) {
                presenter.toLogin();
            } else if (TextUtils.equals(user.getPassword(), ce_login_password.getText().toString().trim())) {//如果输入密码与用户设置密码一致
                presenter.toLogin();
            } else {
                showToast("密码输入错误");
            }

        } else {
            presenter.toLogin();
        }
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getUsername() {
        return ce_login_username.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return ce_login_password.getText().toString().trim();
    }

    /**
     * 登陆成功回调函数  保存用户信息  跳转MainActivity
     */
    @Override
    public void skipToMainAc() {
        SaleUser user = SaleUserModel.getInstance().loadCurrentSaleUsers();
        long sale_id = user.getUser_id() == null ? 0 : user.getUser_id();
        if (sale_id >= 0 && !TextUtils.equals(user.getPhone(), ce_login_username.getText().toString().trim())) {
            SaleUserModel.getInstance().ClearSaler();
            SaleUser saleUser = new SaleUser(System.currentTimeMillis(), System.currentTimeMillis() + "", "昵称", ce_login_username.getText().toString(), "", ce_login_password.getText().toString());
            SaleUserModel.getInstance().insertOrReplace(saleUser);
        } else if (sale_id >= 0 && !TextUtils.isEmpty(user.getPassword())) {//如果有用户信息
            user.setPassword(ce_login_password.getText().toString());
            user.setToken(System.currentTimeMillis() + "");
            SaleUserModel.getInstance().insertOrReplace(user);
        } else {
            SaleUser saleUser = new SaleUser(System.currentTimeMillis(), System.currentTimeMillis() + "", "昵称", ce_login_username.getText().toString(), "", ce_login_password.getText().toString());
            SaleUserModel.getInstance().insertOrReplace(saleUser);
        }
        this.startActivity(new Intent(LoginActivity.this, MainActivity.class));//跳转至MainActivity 并finish掉当前activity
        onBackPressed();
    }

}
